package com.ymatou.iapi.im.testcase;

import com.ymatou.iapi.im.parameter.SendMessageToBuyerBean;
import com.ymatou.iapi.im.parameter.TransferSellerCSSessionBean;
import com.ymatou.iapi.im.service.TransferSellerCSSessionCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ImCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtMessageAttach;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
*买手客服主动转接会话
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_TransferSellerCSSession{
	private static TransferSellerCSSessionBean transfersellercssessionBean;
	private static TransferSellerCSSessionCall transfersellercssessionCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手客服主动转接会话");
	}

	@Before
	public void caseUp() throws IOException {
		transfersellercssessionBean=new TransferSellerCSSessionBean();
		transfersellercssessionCall=new TransferSellerCSSessionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(transfersellercssessionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_TransferSellerCSSession_001() {
		Logger.start(true,"买手客服主动转接会话");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			SendMessageToBuyerBean sendMessageToBuyerBean =ImCallService.initSendMessageToBuyerBean(sellerId,userId,"nihaowohaodajiahao");
			String messageId = ImCallService.SendMessageToBuyer(sendMessageToBuyerBean).get("messageId").getAsString();
			ImDefaultWapper im = new ImDefaultWapper();
			Long LLastMessageTime = Long.parseLong(im.selectTotalCount(sellerId,userId).get(0).get("LLastMessageTime").toString());
			Logger.debug(LLastMessageTime);
			Date lastMessageTime = convertLongTimeToDateTime(LLastMessageTime).toDate();
			Logger.debug(lastMessageTime);

			transfersellercssessionBean.setUserId(userId);
			transfersellercssessionBean.setMasterCSId(sellerId);
			transfersellercssessionBean.setFromCSId(sellerId);
			transfersellercssessionBean.setToCSId(zsellerId);
			transfersellercssessionBean.setLastMessageTime(lastMessageTime);
			transfersellercssessionCall.setData(transfersellercssessionBean);
			transfersellercssessionCall.callService();
			Logger.verifyEquals("Success",transfersellercssessionCall.getString("Status"),"验证Status");
			Map map = im.selectTotalCount(userId,zsellerId).get(0);
			Logger.verifyEquals(userId,Integer.parseInt(map.get("OtherUserId").toString()),"验证当前用户是同一个用户");
			Logger.verifyEquals(zsellerId,Integer.parseInt(map.get("UserId").toString()),"验证是接收方客服");
			YmtMessageAttach ymtMessageAttach = im.selectMessageattachByMessageId(messageId).get(0);
			Logger.debug(ymtMessageAttach);
			String[] str = ymtMessageAttach.getSessionKey().split("_");
			Integer userid1 = Integer.parseInt(str[0]);
			Integer userid2 = Integer.parseInt(str[1]);
			Logger.verifyEquals(userId,userid2,"验证当前用户是同一个用户");
			Logger.verifyEquals(sellerId,userid1,"验证是否是接收方客服");
//			ImRedisWapper redis=new ImRedisWapper();
//			String key = "Ms:" + userId;
//			Set set = redis.getZrange(userId,key);
//			Logger.debug(set);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public DateTime convertLongTimeToDateTime(long longdata) {
		DateTimeFormatter format = DateTimeFormat.forPattern("yyyyMMddHHmmssSSS");
		return DateTime.parse(longdata + "", format);
	}
}